package com.lbloca.locate.weight;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lbloca.locate.R;


public class LoadingDialog extends Dialog {

    private TextView msg;
    private CharSequence mText;
    private AnimationDrawable animDrawable;

    public LoadingDialog(@NonNull Context context) {
        this(context, R.style.myDialog);
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        ImageView img = findViewById(R.id.img_progress);
        animDrawable = (AnimationDrawable) img.getDrawable();
        if (animDrawable != null)
            animDrawable.start();

        if (!TextUtils.isEmpty(mText)) {
            msg = findViewById(R.id.id_tv_loadingmsg);
            if (msg != null)
                msg.setText(mText);
        }
    }

    public void setText(CharSequence text) {
        this.mText = text;
    }

    @Override
    public void dismiss() {
        if (animDrawable != null)
            animDrawable.stop();
        animDrawable = null;
        super.dismiss();
    }

    public LoadingDialog showDialog() {
        show();
        return this;
    }
}
